package ems.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import ems.model.Admin;
import ems.model.Provider;
import ems.service.AdminManager;
import ems.service.ProviderManager;

@ParentPackage("json-default")
@Namespace("")
@Controller("Provider")
@Results(

{ @Result(type = "json") })
public class ProviderAction extends ActionSupport implements ModelDriven {

	private boolean success;
	private String message;
	private String nowDate;
	private List<Provider> ProviderList;

	private Provider provider = new Provider();
	private ProviderManager providerManager;

	// 分页
	private int start;
	private int limit;

	private int total;

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	@JSON(serialize = false)
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	@JSON(serialize = false)
	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String add() throws Exception {

		providerManager.add(provider);

		this.success = true;

		return SUCCESS;

	}

	public String modify() throws Exception {
		providerManager.modify(provider);
		this.success = true;

		return SUCCESS;

	}

	public int deleteByid(int id) throws Exception {
		return providerManager.deleletByid(id);
	}

	public int deleteBatch(int[] ids) throws Exception {
		int deleteNum = 0;
		for (int i = 0; i < ids.length; i++) {
			if (deleteByid(ids[i]) == 1)
				deleteNum++;
		}

		return deleteNum;
	}

	public Provider loadByAid(int aid) throws Exception {
		return providerManager.loadById(aid);
	}

	public String getProviders() throws Exception {
		total = providerManager.getTotalNum();
		ProviderList = providerManager.getProviders(start, limit);
		this.success = true;

		return SUCCESS;
	}

	public String getProvidername() throws Exception {
	
		ProviderList = providerManager.getProviders();
		this.success = true;

		return SUCCESS;
	}
	
	@JSON(serialize = false)
	public Object getModel() {

		return provider;
	}

	/**
	 * @return the admin
	 */
	@JSON(serialize = false)
	public Provider getProvider() {
		return provider;
	}

	/**
	 * @param admin
	 *            the admin to set
	 */

	public void setProvider(Provider provider) {
		this.provider = provider;
	}

	/**
	 * @return the adminManager
	 */

	@JSON(serialize = false)
	public ProviderManager getProviderManager() {
		return providerManager;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	@JSON(serialize = false)
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @param adminManager
	 *            the adminManager to set
	 */

	@Resource
	public void setProviderManager(ProviderManager providerManager) {
		this.providerManager = providerManager;
	}

	public List<Provider> getProviderList() {
		return ProviderList;
	}

	public void setProviderList(List<Provider> providerList) {
		this.ProviderList = providerList;
	}

}
